<template>
  <!-- 附件查看 -->
  <text-base
    :trigger="newValue !== '-' ? 'hover' : ''"
    v-bind="attrs"
    v-model="newValue"
    @itemEvent="itemEvent"
  ></text-base>
</template>

<script setup>
import { computed } from 'vue';
import TextBase from './TextBase.vue';

const attrs = useAttrs();
const emits = defineEmits(['itemEvent']);

const props = defineProps({
  modelValue: {
    type: String,
    default: '',
  },
  fixedValue: {
    type: String,
    default: 'View',
  },
  valueType: {
    type: String,
    default: '',
  },
});

const newValue = computed(() => {
  const { href, attachType, getApiFn, label, row, fileApi } = attrs;
  let flag =
    row[href] || attachType === 'return' || (getApiFn && getApiFn(row)) || (!getApiFn && fileApi);
  return flag ? props.fixedValue || label : '-';
});

function itemEvent() {
  const { href, attachType, row, viewApi, paramsArr, fileApi } = attrs;
  let url = row[href];
  // 返回，自定义事件
  if (attachType === 'return') {
    emits('itemEvent', attrs);
    return;
  }
  if (!url && newValue.value === '-') return;
  let query = {};
  let params = null;
  if (url) {
    // 列表或者详情接口有返回url地址
    params = encodeURIComponent(
      JSON.stringify({
        url,
        viewApi,
      })
    );
  } else {
    // 需要调接口获取url地址
    let param = {};
    if (paramsArr && paramsArr?.length) {
      paramsArr.forEach(item => {
        if (item.definiteValue) param[item.key] = item.definiteValue;
        if (item.value) param[item.key] = row[item.value];
      });
    }
    query = {
      fileApi,
      param: JSON.stringify(param),
    };
    params = encodeURIComponent(JSON.stringify(query));
  }
  emits('itemEvent', { ...attrs, params });
}
</script>
